通过 C# 客户端调用 MATLAB 函数 |
您所在的位置:网站首页 › 定义 external › 通过 C# 客户端调用 MATLAB 函数 |
在文件夹 c:\temp\example 中创建 MATLAB 函数 myfunc。 function [x,y] = myfunc(a,b,c) x = a + b; y = sprintf('Hello %s',c);在您的开发环境中创建 C# 控制台应用程序。在 C# 中引用 MATLAB 类型库的语句如下: MLApp.MLApp matlab = new MLApp.MLApp();以下是完整示例: C# 程序 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { // Create the MATLAB instance MLApp.MLApp matlab = new MLApp.MLApp(); // Change to the directory where the function is located matlab.Execute(@"cd c:\temp\example"); // Define the output object result = null; // Call the MATLAB function myfunc matlab.Feval("myfunc", 2, out result, 3.14, 42.0, "world"); // Display result object[] res = result as object[]; Console.WriteLine(res[0]); Console.WriteLine(res[1]); // Get user input to terminate program Console.ReadLine(); } } }从 C# 客户端程序中,在您的项目中添加对 MATLAB COM 对象的引用。此引用将您的程序绑定到 MATLAB 的特定版本。有关详细信息,请参考您的供应商文档。例如,在 Microsoft Visual Studio 中,打开您的项目。在项目菜单中,选择添加引用。在“添加引用”对话框中,选择 COM 选项卡。选择 MATLAB 应用程序。 在开发环境中编译并运行该应用程序。 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |